/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!				\file		CC_App.cc
				\date		june 2010
				\author		TNick

				\brief		Implements CC_App class


	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	Please read ReadMe.txt and Licence.txt in root folder
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QMessageBox.h>
#include	"QCoreApplication.h"

#include	<assert.h>

#include	<main.h>
#include	"CC_App.h"
#include	<gui/cust/toolbars/cst_tlb.h>
#include	<gui/cust/menus/cst_mnu.h>
#include	<gui/cust/actions/cst_act.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

unsigned			CC_App::uApp = 0;
QApplication*		CC_App::QT_App;
CC_HDL2DN			CC_App::Drawings;
CC_HDL2DN			CC_App::PlugIns;
CC_HDL2D			CC_App::Toolbars;
CC_HDL2D			CC_App::Menus;
CC_HDL2D			CC_App::Actions;


unsigned			uAppErrCode;
MainWindow*			g_MW;

/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */


///* ------------------------------------------------------------------------- */
///* constructor	                                                             */
//CC_App::CC_App()
//{
//
//
//
//}
///* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* lander                                                                    */
void		CC_App::Landing	(void)
{

	/* iterate in chain of drawings and free any one that's left inhere */
	CC_Draw*	pItrDraw = (CC_Draw*)Drawings.first();
	while (pItrDraw != NULL)
		{
			delete pItrDraw;
			pItrDraw = (CC_Draw*)Drawings.first();
		}

	/* iterate in chain of plugins and free any one that's left inhere */
	CC_PlugIn*	pItrPlgIn = (CC_PlugIn*)PlugIns.first();
	while (pItrPlgIn != NULL)
		{
			delete pItrPlgIn;
			pItrPlgIn = (CC_PlugIn*)PlugIns.first();
		}

	/* now close error system */
	CC_Err::EndErrSys();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		CC_App::TakeOff		(QApplication* qtApp)
{
	bool		bRet = false;

	/* at this stage, application is OK */
	uAppErrCode = 0;
	g_MW = NULL;

	/* we must enshure that this is the only instance */
	Q_ASSERT(uApp == 0);

	/* we're up */
	uApp = 1;

	/* prepare the ground for easy access to settings */
	QCoreApplication::setOrganizationName("TNick");
	QCoreApplication::setOrganizationDomain("sites.google.com/site/tnickweb/");
	QCoreApplication::setApplicationName("CASP");

	/* initialize error system; using of CC_Err is only
	allowed after this point */
	if (!CC_Err::InitErrSys())
		{
			QMessageBox::critical(NULL,
								  QMessageBox::tr("CASP failed to start"),
								  QMessageBox::tr("The most basic subsystem (Error reporting)\n"
												  "could not be started.\n"
												  "Please retry!"),
								  QMessageBox::Ok, QMessageBox::NoButton );
			uAppErrCode = 1;
			return false;
		}

	CC_Err		LcErr;


	/* save a pointer for later use */
	QT_App = qtApp;

	/* prepare toolbars and actions for use */
	//g_initActionSystem(&LcErr);
	//g_initToolbarsSystem(&LcErr);

	/* create GUI; it if fails, there's not much purpose for us anymore */
	g_MW = new MainWindow(&LcErr);
	if (!g_MW->InitOK())
		goto	Have_Errors;


	/* error system is ready to be fully functional */
	CC_Err::CruiseErrSys();

	g_MW->show();
	LcErr.Show();
	bRet = true;
	Exit_Point:
	return bRet;
	Have_Errors:
	LcErr.Show();
	goto Exit_Point;
}
/* ========================================================================= */

/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
